<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class ProductController extends AdminbaseController {
	

	
	function _initialize() {
		parent::_initialize();
		$this->db = D("Product");


	}


	function index(){
		

		$order = empty ( $data ['order'] ) ? "top DESC,listorder DESC,id desc" : $data ['order'];




		if(I("get.keyword")){

			$map['title']=array('like','%'.I("get.keyword").'%');

		}

		if(I("get.fengge")){

			$map['fengge']=I("get.fengge");

		}

		if(I("get.zuhe")){

			$map['zuhe']=I("get.zuhe");

		}
		if(I("get.brand")){

			$map['zuhe']=I("get.zuhe");

		}






		$count=$this->db->where($map)->count();
		$page = $this->page($count, 20);
		$list=$this->db->where($map)->limit($page->firstRow . ',' . $page->listRows)->order ( $order)->select();

		$this->assign("Page",$page->show(""));

		$this->assign("list",$list);
		
		$this->display();
	}
	
	function add(){
		
		
		if(IS_POST){




			$_POST["thumbs"]=implode(",",$_POST["thumbs"]);




			if ($this->db->create($_POST)) {
				$product_id=$this->db->add();

				if ($product_id!==false) {






					$this->success("添加成功！", U("index",array("product_id"=>$product_id)));
				} else {
					$this->error("添加失败！");
				}
			} else {
				$this->error($this->db->getError());
			}





            
			
			
		}else{
			
		

			
	
			$this->display("form");
			
		}
		

	
	}




	
	public function edit(){


		if(IS_POST){

			$_POST["thumbs"]=implode(",",$_POST["thumbs"]);


			if ($this->db->create($_POST)) {



				if ($this->db->save()!==false) {


					$this->success("保存成功！");
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->db->getError());
			}

		}else{

			$id= intval(I("get.id"));
			if(!$id){
				$this->error("缺少参数！");
			}

			$r=$this->db->where("id=$id")->find();


			if($r["thumbs"]){
				$r["thumbs"]=explode(",",$r["thumbs"]);

			}


			$this->assign("edit",true);
			$this->assign("data",$r);
			$this->display("form");
		}
	}






	//排序
	public function listorders() {
		$status = parent::_listorders($this->db);
		if ($status) {
			$this->success("排序更新成功！");
		} else {
			$this->error("排序更新失败！");
		}
	}

	function delete(){
		if(isset($_GET['id'])){
			$id = intval(I("get.id"));


			if ($this->db->delete($id)) {

				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		}



		if(isset($_POST['ids'])){
			$ids=I("param.ids");

			$map[id]=array("in",$ids);

			if ($this->db->where($map)->delete()) {
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		}





	}
	
	

	

	
	
}